import React, {PureComponent} from 'react';
import {QueryTreeTable, GlobalProvider} from 'jh-lib';
import ComponentType from "../../Config/ComponentType";

const request = {
    reqGet: (api, data) => {
        return new Promise((resolve, reject) => {
            setTimeout(() => {

                const {maxResults, startIndex} = data;
                let record = [];
                for (let i = startIndex; i < (startIndex + maxResults); i++) {
                    record.push({id: i, name: '用户' + i, age: 123})
                }
                resolve({
                    status: 200, data: {
                        resultCount: 100, maxResults: maxResults,
                        records: record
                    }
                })
            }, 2000);
        })
    },
};
const service = {
    pageFindByFinderCode: (data) => {
        return new Promise((resolve, reject) => {
            setTimeout(() => {
                let {searchText, startIndex, maxResults} = data;
                console.log('searchText', searchText)
                let res = [];
                if (searchText) {
                    resolve({
                        status: 200, data: {
                            results: res,
                            maxResults: res.length,
                            resultCount: 0,
                        }
                    })
                } else {
                    for (let i = 0; i < maxResults; i++) {
                        let obj = {key: i + 1, id: startIndex + i + 1, name: '用户' + (startIndex + i + 1)};
                        res.push(obj)
                    }
                    resolve({
                        status: 200, data: {
                            results: res,
                            maxResults: res.length,
                            resultCount: 200,
                        }
                    })
                }

            }, 2000);
        })
    },
    objectBoxFetch: (data) => {
        console.log('请求参数', data);
        return new Promise((resolve, reject) => {
            setTimeout(() => {
                let res = [];
                for (let i = 0; i < 100; i++) {
                    let obj = {key: i + 1, id: i + 1, name: '用户' + (i + 1), code: 10 + i + `${i + 1}`};
                    res.push(obj)
                }
                resolve({
                    status: 200, data: {
                        records: res,
                        maxResults: res.length,
                        resultCount: 200,
                    }
                })
            }, 1000)
        })
    }
};
const compData = [
    {id: '1', name: '测试1'}, {id: '2', name: '测试2'}, {id: '3', name: '测试3'}, {id: '4', name: '测试4'},
];

export default class Test extends React.Component {

    dataComp = [
        {
            compLabel: '查找器',
            componentType: ComponentType.FINDER,
            fieldName: 'object',
        },
        {
            componentType: ComponentType.COMBO_BOX,
            fieldName: 'combo',
            compLabel: '枚举',
        },
        {
            componentType: ComponentType.CALENDAR,
            fieldName: 'date',
            compLabel: '选择时间',
        },
        {
            componentType: ComponentType.TEXT_INPUT,
            fieldName: 'text',
            compLabel: '文本',
        },
        {
            componentType: ComponentType.NUMBER_INPUT,
            fieldName: 'number',
            compLabel: '数字',
        },
        {
            componentType: ComponentType.CHECK_BOX,
            fieldName: 'checkbox',
            compLabel: '开关',
        },
    ];

    render() {
        return <>
            <GlobalProvider request={request}>

                <div style={{border: '1px solid #ddd', height: 400, overflow: 'auto'}}>
                    <QueryTreeTable
                        style={{height: '100%'}}
                        appCode={'USER'}
                        columns={[{dataIndex: 'names', title: '名称s', width: 100}, {
                            dataIndex: 'name',
                            title: '名称',
                            width: 100
                        }, {dataIndex: 'age', title: '年龄', width: 100}]}
                        fetchList={service.objectBoxFetch}
                        needNO={true}
                        rowSelect={true}
                        filterConfig={this.dataComp}
                    />
                </div>
            </GlobalProvider>
        </>
    }
}

